﻿using System;
using System.ServiceModel;
using System.Windows.Forms;
using System.ServiceModel.Channels;

namespace DMSClient
{
    public partial class Login : Form
    {

        private TextBox tbReturnRol;
        private TextBox tbReturnUser;
        

        public Login()
        {
            InitializeComponent();
        }

        public Login(ref TextBox tbRol, ref TextBox tbUser) {
            InitializeComponent();
            tbReturnRol = tbRol;
            tbReturnUser = tbUser;

        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            int ValidUser;

            WSHttpBinding binding = new WSHttpBinding();
            binding.CloseTimeout = new TimeSpan(0, 1, 0);
            binding.OpenTimeout = new TimeSpan(0, 1, 0);
            binding.ReceiveTimeout = new TimeSpan(0, 10, 0);
            binding.SendTimeout = new TimeSpan(0, 1, 0);
            binding.Security.Mode = SecurityMode.None;

            try
            {
                EndpointAddress endpoint = new EndpointAddress(uteServer.Text.Trim());
                DMSServices.DMSServicesClient UserSrv = new DMSServices.DMSServicesClient(binding, endpoint);

                UserSrv.Open();
                ValidUser = UserSrv.validateUser(this.txtUser.Text, this.txtPass.Text);
                UserSrv.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "Error establishing connection to the server", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (ValidUser>=1)
            {
                Properties.Settings.Default.Server = uteServer.Text.Trim();
                Properties.Settings.Default.Save();
                tbReturnRol.Text = ValidUser.ToString();
                tbReturnUser.Text = txtUser.Text;
                this.DialogResult = DialogResult.OK;   
            }
            else if (ValidUser == 0)
            {
                MessageBox.Show(this, "Invalid user", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else {
                MessageBox.Show(this, "Server side error" , this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Login_Load(object sender, EventArgs e)
        {
            uteServer.Text = Properties.Settings.Default.Server;
        }

        private void ultraButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtUser_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) txtPass.Focus();
        }

        private void txtPass_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) {
                if (txtPass.Text.Equals(""))
                {
                    txtUser.Focus();
                }
                else {
                    if(!txtPass.Text.Equals("")) ubSubmit.PerformClick();
                }
            }
        }

        private void Login_Shown(object sender, EventArgs e)
        {
            txtUser.Focus();
        }

        private void ultraGroupBox1_Click(object sender, EventArgs e)
        {

        }

        private void txtPass_ValueChanged(object sender, EventArgs e)
        {

        }
    }
}
